<template>
  <el-dialog v-model="show" width="1000px" :title="title" append-to-body :close-on-click-modal="false">
    <el-form
      ref="hotelRef" :model="form" :rules="rules" label-width="80px" :inline="true"
      :disabled="isView"
    >
      <el-row :gutter="20">
        <el-col>
          <el-form-item label="酒店图片" prop="photos">
            <image-upload2 v-model="form.photos" :disabled="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="酒店名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" placeholder="请输入酒店名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="酒店星级" prop="starLevel" style="width: 100%">
            <el-select v-model="form.starLevel" placeholder="请选择酒店星级" style="width: 100%">
              <el-option label="一星" value="1" />
              <el-option label="二星" value="2" />
              <el-option label="三星" value="3" />
              <el-option label="四星" value="4" />
              <el-option label="五星" value="5" />
              <el-option label="六星" value="6" />
              <el-option label="七星" value="7" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="contact" style="width: 100%">
            <el-input v-model="form.contact" placeholder="请输入联系方式" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks" style="width: 100%">
            <el-input v-model="form.remarks" type="textarea" rows="3" placeholder="备注" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入住时间" prop="checkInTime" style="width: 100%">
            <el-time-select v-model="form.checkInTime" placeholder="入住时间" start="00:00" end="24:00" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="离店时间" prop="checkOutTime" style="width: 100%">
            <el-time-select v-model="form.checkOutTime" start="00:00" end="24:00" placeholder="离店时间" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="酒店地址" prop="address" style="width: 100%">
            <el-cascader
              v-model="regionIds"
              style="width:100%"
              :options="regionsList"
              :props="{ label:'text'}"
              placeholder="请选择省市区"
            />
            <el-input v-model="form.address" type="textarea" placeholder="请输入详细地址" rows="3" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标签" prop="label" style="width: 100%">
            <el-input v-model="tagName" type="text" @keyup.enter="addTag">
              <template #append>
                <el-button type="primary" @click="addTag">添加</el-button>
              </template>
            </el-input>
            <div>
              <el-tag
                v-for="tag in tagList" :key="tag" class="mx-1" closable type="info"
                @close="removeTag(tag)"
              >{{ tag }}</el-tag>
            </div>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="酒店简介" prop="introduction" style="width: 100%;">
            <u-editor v-model="form.introduction" placeholder="请输入酒店简介" :disabled="isView" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!isView" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="show=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>

import { getCurrentInstance, ref, toRefs, watch } from 'vue'
import { addHotel, updateHotel } from '@/api/logex/hotel'
import useVenStore from '@/store/modules/ven'
import { reactive } from '@vue/runtime-core'
import { computed } from '@vue/reactivity'
import useUserStore from '@/store/modules/user'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['submit'])

const show = ref(false)
const title = ref('')
const buttonLoading = ref(false)
const regionIds = ref([])
const tagName = ref('')
const tagList = ref([])
const isView = ref(false)

const data = reactive({
  form: {
    id: null,
    venueId: null,
    name: null,
    starLevel: null,
    contact: null,
    remarks: null,
    photos: null,
    checkInTime: null,
    checkOutTime: null,
    province: null,
    city: null,
    area: null,
    address: null,
    label: null,
    introduction: null,
    status: '0',
    delStatus: '0'
  },
  rules: {
    name: [
      { required: true, message: '酒店名称不能为空', trigger: 'blur' }
    ],
    starLevel: [
      { required: true, message: '酒店星级不能为空', trigger: 'change' }
    ],
    // contact: [
    //   { required: true, message: '联系方式不能为空', trigger: 'blur' }
    // ],
    photos: [
      { required: true, message: '酒店图片不能为空', trigger: 'blur' }
    ],
    checkInTime: [
      { required: true, message: '入住时间不能为空', trigger: 'change' }
    ],
    checkOutTime: [
      { required: true, message: '离店时间不能为空', trigger: 'change' }
    ],
    address: { validator: (rule, value, callback) => {
      if (!form.value.address || regionIds.value.length == 0) {
        callback(new Error('酒店地址不能为空'))
      } else {
        callback()
      }
    }, required: true, trigger: 'blur' },
    label: { validator: (rule, value, callback) => {
      if (tagList.value.length == 0) {
        callback('标签不能为空')
      } else {
        callback()
      }
    }, required: true, trigger: 'blur' }
  }
})
const { form, rules } = toRefs(data)

const regionsList = computed(() => useUserStore().regionsList)

function submitForm() {
  proxy.$refs['hotelRef'].validate(valid => {
    if (valid) {
      form.value.label = tagList.value.join('，')
      form.value.province = regionIds.value[0]
      form.value.city = regionIds.value[1]
      form.value.area = regionIds.value[2]

      buttonLoading.value = true
      if (form.value.id != null) {
        updateHotel(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          show.value = false
          emit('submit')
        }).finally(() => {
          buttonLoading.value = false
        })
      } else {
        addHotel(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          show.value = false
          emit('submit')
        }).finally(() => {
          buttonLoading.value = false
        })
      }
    }
  })
}

watch(show, (val) => {
  if (!val) {
    reset()
  }
})

function reset() {
  form.value = {
    id: null,
    venueId: null,
    name: null,
    starLevel: null,
    contact: null,
    remarks: null,
    photos: null,
    checkInTime: null,
    checkOutTime: null,
    province: null,
    city: null,
    area: null,
    address: null,
    label: null,
    introduction: null,
    status: '0',
    delStatus: '0'
  }
  proxy.resetForm('hotelRef')
}

function addTag() {
  if (tagName.value) {
    if (tagList.value.indexOf(tagName.value) == -1) {
      tagList.value.push(tagName.value)
    }
    tagName.value = ''
  }
}

function removeTag(tag) {
  tagList.value.splice(tagList.value.indexOf(tag), 1)
}

function open(params) {
  reset()

  show.value = true
  title.value = params.title
  isView.value = params.isView || false

  if (params.hotel) {
    form.value = params.hotel
    tagList.value = params.hotel.label.split('，')

    regionIds.value = []
    if (params.hotel.province) {
      regionIds.value.push(params.hotel.province)
    }
    if (params.hotel.city) {
      regionIds.value.push(params.hotel.city)
    }
    if (params.hotel.area) {
      regionIds.value.push(params.hotel.area)
    }
  } else {
    form.value.venueId = useVenStore().venueInfo.id
    tagList.value = []
    regionIds.value = []
  }
}
defineExpose({
  open
})
</script>

<style scoped>
.mx-1{margin: 5px}
</style>
